using UnityEditor;
using UnityEngine;

namespace Jerry.UiFrame.Editor
{
    public class CreateAsset : EditorWindow
    {
        private static string _prefabsPath = "Assets/Resources/UIPrefabs/";
        private static string _baseClassPath = "Assets/Scripts/UI/Base/";
        private static string _uiScriptsPath = "Assets/Scripts/UI/Win/";
        
        private static GUIStyle _style;
        private static GUIStyle _style2;
        private static GUIStyle _style3;
        public static void ShowWindow()
        {
            var window = GetWindow<CreateAsset>("创建UI配置文件");
            _style = new GUIStyle();
            _style.normal.textColor = Color.red;
            _style2 = new GUIStyle();
            _style2.normal.textColor = Color.yellow;
            
            _style3 = new GUIStyle();
            _style3.fontSize = 15;
            _style3.normal.textColor = Color.red;
        }
        public void OnGUI()
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginVertical(EditorStyles.helpBox);
                {
                    GUILayout.Label("需要创建配置文件才能生成预制体和脚本！！！", _style2);
                    GUILayout.Space(5f);

                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("预制体存放位置:");
                        _prefabsPath = GUILayout.TextField(_prefabsPath);
                    }
                    GUILayout.EndHorizontal();
                }

                GUILayout.Label("如果存放位置不在Resources下，请自定义加载器(JLoader)!!!", _style);
                
                GUILayout.Space(10f);
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label("BaseUI脚本存放位置");
                    _baseClassPath = GUILayout.TextArea(_baseClassPath);
                }
                GUILayout.EndHorizontal();
                
                GUILayout.Space(5f);
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label("UI逻辑脚本存放位置");
                    _uiScriptsPath = GUILayout.TextField(_uiScriptsPath);
                }
                GUILayout.EndHorizontal();
                
                GUILayout.Space(30f);
                if (GUILayout.Button("创建UiPathConfig.asset"))
                {
                    CreateUiPathAsset();
                    EditorUtility.DisplayDialog("⚠️", "创建成功，请重新生成！！", "确认", "取消");
                    Close();
                }
                GUILayout.EndVertical();
                
                GUILayout.Space(10f);
                GUILayout.Label("⚠️！！！请将Packages -> UI Frame -> Editor ->\n" +
                                " UIPrefabs -> 2DCanvas复制到Resources文件夹下",
                    _style3);
            }
            GUILayout.EndVertical();
        }
        
        /// <summary>
        /// 创建保存路径的Asset
        /// </summary>
        static void CreateUiPathAsset()
        {
            var path = "Assets/Resources/UiPathConfig.asset";
            var asset = AssetDatabase.LoadAssetAtPath<AllPaths>(path);
            if (asset == null)
            {
                asset = CreateInstance<AllPaths>();
                asset.prefabPath = _prefabsPath;
                asset.baseUiScriptsPath = _baseClassPath;
                asset.uiScriptsPath = _uiScriptsPath;
                AssetDatabase.CreateAsset(asset, path);
            }

            AssetDatabase.SaveAssets();
        }
    }
}